home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / ButtonFilter.java < prev    next >
Text File  |  1998-09-15  |  8KB  |  246 lines

  1. /*
  2.  * @(#)ButtonFilter.java    1.8 98/03/18
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.applet.Applet;
  32. import java.awt.Image;
  33. import java.awt.Graphics;
  34. import java.awt.Rectangle;
  35. import java.util.StringTokenizer;
  36. import java.util.Vector;
  37. import java.util.Hashtable;
  38. import java.net.URL;
  39. import java.awt.image.*;
  40. import java.net.MalformedURLException;
  41.  
  42. /**
  43.  * An extensible ImageMap applet class.
  44.  * The active areas on the image are controlled by ImageArea classes
  45.  * that can be dynamically loaded over the net.
  46.  *
  47.  * @author     Jim Graham
  48.  * @version     1.8, 03/18/98
  49.  */
  50. class ButtonFilter extends RGBImageFilter {
  51.     boolean pressed;
  52.     int defpercent;
  53.     int border;
  54.     int width;
  55.     int height;
  56.  
  57.     ColorModel models[] = new ColorModel[7];
  58.     ColorModel origbuttonmodel;
  59.  
  60.     public ButtonFilter(boolean press, int p, int b, int w, int h) {
  61.     pressed = press;
  62.     defpercent = p;
  63.     border = b;
  64.     width = w;
  65.     height = h;
  66.     }
  67.  
  68.     public void setHints(int hints) {
  69.     super.setHints(hints & (~ImageConsumer.COMPLETESCANLINES));
  70.     }
  71.  
  72.     public void setColorModel(ColorModel model) {
  73.     if (model instanceof IndexColorModel && true) {
  74.         IndexColorModel icm = (IndexColorModel) model;
  75.         models[0] = filterIndexColorModel(icm, false, false, 0);
  76.         models[1] = filterIndexColorModel(icm, true, !pressed, defpercent);
  77.         models[2] = null;
  78.         if (pressed) {
  79.         models[3] = filterIndexColorModel(icm, true, false,
  80.                           defpercent/2);
  81.         } else {
  82.         models[3] = models[0];
  83.         }
  84.         models[4] = null;
  85.         models[5] = filterIndexColorModel(icm, true, pressed, defpercent);
  86.         models[6] = models[0];
  87.         origbuttonmodel = model;
  88.         consumer.setColorModel(models[3]);
  89.     } else {
  90.         super.setColorModel(model);
  91.     }
  92.     }
  93.  
  94.     public IndexColorModel filterIndexColorModel(IndexColorModel icm,
  95.                          boolean opaque,
  96.                          boolean brighter,
  97.                          int percent) {
  98.     byte r[] = new byte[256];
  99.     byte g[] = new byte[256];
  100.     byte b[] = new byte[256];
  101.     byte a[] = new byte[256];
  102.     int mapsize = icm.getMapSize();
  103.     icm.getReds(r);
  104.     icm.getGreens(g);
  105.     icm.getBlues(b);
  106.     if (opaque) {
  107.         icm.getAlphas(a);
  108.         for (int i = 0; i < mapsize; i++) {
  109.         int rgb = filterRGB(icm.getRGB(i), brighter, percent);
  110.         a[i] = (byte) (rgb >> 24);
  111.         r[i] = (byte) (rgb >> 16);
  112.         g[i] = (byte) (rgb >> 8);
  113.         b[i] = (byte) (rgb >> 0);
  114.         }
  115.     }
  116.     return new IndexColorModel(icm.getPixelSize(), mapsize, r, g, b, a);
  117.     }
  118.  
  119.     /**
  120.      * Define the ranges of varying highlight for the button.
  121.      * ranges is an array of 8 values which split up a scanline into
  122.      * 7 different regions of highlighting effect:
  123.      *
  124.      * ranges[0-1] = area outside of left edge of button
  125.      * ranges[1-2] = area inside UpperLeft highlight region left of center
  126.      * ranges[2-3] = area requiring custom highlighting left of center
  127.      * ranges[3-4] = area inside center of button
  128.      * ranges[4-5] = area requiring custom highlighting right of center
  129.      * ranges[5-6] = area inside LowerRight highlight region right of center
  130.      * ranges[6-7] = area outside of right edge of button
  131.      * ranges[8-9] = y coordinates for which these ranges apply
  132.      *
  133.      * Note that ranges[0-1] and ranges[6-7] are empty where the edges of
  134.      * the button touch the left and right edges of the image (everywhere
  135.      * on a square button) and ranges[2-3] and ranges[4-5] are only nonempty
  136.      * in those regions where the UpperLeft highlighting has leaked over
  137.      * the "top" of the button onto parts of its right edge or where the
  138.      * LowerRight highlighting has leaked under the "bottom" of the button
  139.      * onto parts of its left edge (can't happen on square buttons, happens
  140.      * occasionally on round buttons).
  141.      */
  142.     public void buttonRanges(int y, int ranges[]) {
  143.     ranges[0] = ranges[1] = 0;
  144.     if (y < border) {
  145.         ranges[2] = ranges[3] = ranges[4] = ranges[5] = width - y;
  146.         ranges[8] = ranges[9] = y;
  147.     } else if (y > height - border) {
  148.         ranges[2] = ranges[3] = ranges[4] = ranges[5] = height - y;
  149.         ranges[8] = ranges[9] = y;
  150.     } else {
  151.         ranges[2] = ranges[3] = border;
  152.         ranges[4] = ranges[5] = width - border;
  153.         ranges[8] = border;
  154.         ranges[9] = height - border;
  155.     }
  156.     ranges[6] = ranges[7] = width;
  157.     }
  158.  
  159.     private int savedranges[];
  160.  
  161.     protected int[] getRanges(int y) {
  162.     int ranges[] = savedranges;
  163.     if (ranges == null) {
  164.         ranges = savedranges = new int[10];
  165.         ranges[8] = ranges[9] = -1;
  166.     }
  167.     if (y < ranges[8] || y > ranges[9]) {
  168.         buttonRanges(y, ranges);
  169.     }
  170.     return ranges;
  171.     }
  172.  
  173.     public void setPixels(int x, int y, int w, int h,
  174.               ColorModel model, byte pixels[], int off,
  175.               int scansize) {
  176.     if (model == origbuttonmodel) {
  177.         int ranges[] = getRanges(y);
  178.         int x2 = x + w;
  179.         int y2 = y + h;
  180.         for (int cy = y; cy < y2; cy++) {
  181.         if (cy < ranges[8] || cy > ranges[9]) {
  182.             buttonRanges(cy, ranges);
  183.         }
  184.         for (int i = 0; i < 7; i++) {
  185.             if (x2 > ranges[i] && x < ranges[i+1]) {
  186.             int cx1 = Math.max(x, ranges[i]);
  187.             int cx2 = Math.min(x2, ranges[i+1]);
  188.             if (models[i] == null) {
  189.                 super.setPixels(cx1, cy, cx2 - cx1, 1,
  190.                         model, pixels,
  191.                         off + (cx1 - x), scansize);
  192.             } else {
  193.                 if (cx1 < cx2) {
  194.                 consumer.setPixels(cx1, cy, cx2 - cx1, 1,
  195.                            models[i], pixels,
  196.                            off + (cx1 - x), scansize);
  197.                 }
  198.             }
  199.             }
  200.         }
  201.         off += scansize;
  202.         }
  203.     } else {
  204.         super.setPixels(x, y, w, h, model, pixels, off, scansize);
  205.     }
  206.     }
  207.  
  208.     public int filterRGB(int x, int y, int rgb) {
  209.     boolean brighter;
  210.     int percent;
  211.     if ((x < border && y < height - x) || (y < border && x < width - y)) {
  212.         brighter = !pressed;
  213.         percent = defpercent;
  214.     } else if (x >= width - border || y >= height - border) {
  215.         brighter = pressed;
  216.         percent = defpercent;
  217.     } else if (pressed) {
  218.         brighter = false;
  219.         percent = defpercent / 2;
  220.     } else {
  221.         return rgb & 0x00ffffff;
  222.     }
  223.     return filterRGB(rgb, brighter, percent);
  224.     }
  225.  
  226.     public int filterRGB(int rgb, boolean brighter, int percent) {
  227.     int r = (rgb >> 16) & 0xff;
  228.     int g = (rgb >> 8) & 0xff;
  229.     int b = (rgb >> 0) & 0xff;
  230.     if (brighter) {
  231.         r = (255 - ((255 - r) * (100 - percent) / 100));
  232.         g = (255 - ((255 - g) * (100 - percent) / 100));
  233.         b = (255 - ((255 - b) * (100 - percent) / 100));
  234.     } else {
  235.         r = (r * (100 - percent) / 100);
  236.         g = (g * (100 - percent) / 100);
  237.         b = (b * (100 - percent) / 100);
  238.     }
  239.     return (rgb & 0xff000000) | (r << 16) | (g << 8) | (b << 0);
  240.     }
  241.  
  242.   public String getAppletInfo() {
  243.     return "Title: ButtonFilter \nAuthor: Jim Graham \nAn extensible ImageMap applet class.  The active areas on the image are controlled by ImageArea classes that can be dynamically loaded over the net.";
  244.   }
  245. }
  246.